package com.gill.touka.sentinel.origin;

import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * sentinel扩展：区分来源
 * 测试方法：
 * 1、访问：localhost:8082/shares/1 会抛出异常“origin must be specified”
 * 2、访问：localhost:8082/shares/1?origin=gill，能够正常访问，才会被sentinel控制台监控
 */
//@Component//关闭来源，防止影响其他特性的测试
public class MyRequestOriginParser implements RequestOriginParser {

    @Override
    public String parseOrigin(HttpServletRequest request) {
        String origin = request.getParameter("origin");//没有origin参数就不能访问
        if(StringUtils.isBlank(origin)){
            throw new IllegalArgumentException("origin must be specified");
        }
        return origin;
    }
}
